package com.hnkywl.sts.dao.report;

import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import oracle.jdbc.OracleCallableStatement;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.*;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-6-22
 * Time: 下午10:02
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class YqyskDao extends SimpleHibernateTemplate {
    /**
     * 应收款统计（包括统计页面）
     * @param type  TYPE=0 表示回款计划新增页面的查询  TYPE=1  表示逾期收款查询
     * @param gsid
     * @param bmid
     * @param startDate
     * @param endDate
     * @return
     */
    public Map<String, List> ysktjList(Integer type,Long gsid, Long bmid, Date startDate, Date endDate) {
        if (gsid == null)
            gsid = -1L;
        if (bmid == null)
            bmid = -1L;
        //开始时间
        java.sql.Date pStartDate = new java.sql.Date(startDate.getTime());
        //结束时间
        java.sql.Date pEndDate = new java.sql.Date(endDate.getTime());
        //当前时间
        java.sql.Date pNowDate = new java.sql.Date(new Date().getTime());
        Connection con = null;
        CallableStatement call = null;
        ResultSet rs = null;
        try {
            con = getSession().connection();
            call = con.prepareCall("{Call RPT_CW_HKJH(?,?,?,?,?,?,?)}");
            call.setInt(1, type);
            call.setLong(2, gsid);
            call.setLong(3, bmid);
            call.setDate(4, pNowDate);
            call.setDate(5, pStartDate);
            call.setDate(6, pEndDate);
            call.registerOutParameter(7, oracle.jdbc.OracleTypes.CURSOR);
            call.execute();
            //====================================
            rs = ((OracleCallableStatement) call).getCursor(7);
            ResultSetMetaData resultsMetaData = rs.getMetaData();
            int columnCount = resultsMetaData.getColumnCount();
            Map<String, List> result = new HashMap<String, List>();
            List<Map> rows = new ArrayList<Map>();
            List<Map> footer = new ArrayList<Map>();
            while (rs.next()) {
                Map<String, Object> r = new HashMap<String, Object>();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = resultsMetaData.getColumnName(i).toLowerCase();
                    r.put(columnName, rs.getObject(i));
                }
                if ("1".equals(r.get("flag").toString()))
                    rows.add(r);
                else
                    footer.add(r);
            }
            result.put("rows", rows);
            result.put("footer", footer);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } finally {
            closeResource(rs, call, con);
        }
    }

    private void closeResource(ResultSet rs, Statement s, Connection con) {
        if (rs != null)
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        if (s != null)
            try {
                s.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        if (con != null)
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
    }
}
